﻿using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.IO;

namespace CommonUtils
{
    [SwaggerSchema("回调结果")]
    public class ApiResult
    {
        [SwaggerSchema("状态")]
        public bool Success { get; set; } = true;

        [SwaggerSchema("状态码")]
        public int Code { get; set; } = 0;

        [SwaggerSchema("消息")]
        public string Message { get; set; } = "Success";

        [SwaggerSchema("时间")]
        public DateTime Time { get; } = DateTime.Now;

        public ApiResult() { }

        public ApiResult(bool success = true, int code = 0, string message = "Success")
        {
            Success = success;
            Code = code;
            Message = message;
        }

        public static readonly ApiResult Ok_ = ApiResult<object>.Ok_;

        public static ApiResult Ok() => Ok_;

        public static ApiResult<T> Ok<T>(T data) => ApiResult<T>.Ok(data);

        public static ApiResult Msg(string msg) => ApiResult<object>.Msg(msg);

        public static readonly ApiResult Error_ = ApiResult<object>.Error_;

        public static ApiResult Error() => Error_;

        public static ApiResult Error(string message) => ApiResult<object>.Error(message);

        public static ApiResult Error(int code, string message) => ApiResult<object>.Error(code, message);

        public static readonly ApiResult Unlogin = ApiResult<object>.Unlogin;

        public static readonly ApiResult ArgsError = ApiResult<object>.ArgsError;

        public static readonly ApiResult ParamsError = ArgsError;

        public static readonly ApiResult NoData = ApiResult<object>.NoData;

        public static FileResult File(string path)
        {
            if (FileUtil.Exists(path))
                return new FileStreamResult(FileUtil.OpenRead(path), ContentType.Stream) { FileDownloadName = path };
            return new FileStreamResult(new MemoryStream(), ContentType.Stream) { FileDownloadName = path };
        }
    }

    [SwaggerSchema("回调结果")]
    public class ApiResult<T> : ApiResult
    {
        [SwaggerSchema("数据")]
        public T Data { get; set; } = default;

        public ApiResult() { }

        public ApiResult(bool success = true, int code = 0, string message = "Success", T data = default)
        : base(success, code, message)
        {
            Data = data;
        }

        public static readonly new ApiResult<T> Ok_ = new ApiResult<T>();

        public static new ApiResult<T> Ok() => Ok_;

        public static ApiResult<T> Ok(T data) => new ApiResult<T>(true, 0, "Success", data);

        public static new ApiResult<T> Msg(string msg) => new ApiResult<T>(true, 0, msg);

        public static readonly new ApiResult<T> Error_ = new ApiResult<T>(false, -1, "服务器异常");

        public static new ApiResult<T> Error() => Error_;

        public static new ApiResult<T> Error(string message) => new ApiResult<T>(false, -1, message);

        public static new ApiResult<T> Error(int code, string message) => new ApiResult<T>(false, code, message);

        public static readonly new ApiResult<T> Unlogin = new ApiResult<T>(false, -2, "登录状态失效");

        public static readonly new ApiResult<T> ArgsError = new ApiResult<T>(false, -3, "参数异常");

        public static readonly new ApiResult<T> ParamsError = ArgsError;

        public static readonly new ApiResult<T> NoData = new ApiResult<T>(false, -4, "无数据");
    }
}
